﻿var isTop = self == top;
if (!isTop) {
    var openTab = null;
    var closeTab = null;
    var refreshTab = null;
    var openMTab = null;
    var closeMTab = null;
    var refreshMTab = null;
    var activeMTab = null;
    var activeTab = null;
    var refreshAndActiveMTab = null;
    var refreshAndActiveTab = null;
}
$(function () {
    var isTop = self == top;
    //alert("isTop = " + isTop + "\nlocation = " + window.location);
    if (isTop) {
        return;
    }
    openTab = top.openTab;
    closeTab = top.closeTab;
    refreshTab = top.refreshTab;
    openMTab = top.openMTab;
    closeMTab = top.closeMTab;
    refreshMTab = top.refreshMTab;
    activeTab = top.activeTab;
    activeMTab = top.activeMTab;
    refreshAndActiveMTab = top.refreshAndActiveMTab;
    refreshAndActiveTab = top.refreshAndActiveTab;
});
function createWin(opt) {
    var bIsTop = self == top;
    if (!bIsTop) {
        if (typeof (opt) == "object" && typeof (opt.isInTop) == "boolean" && opt.isInTop) {
            top.currentWin = window;
            top.createWin(opt);
            return;
        }
    }
    var winId = "globall_pop_win_9981";
    if (typeof (opt) == "object" && typeof (opt.winId) == "string") {
        winId = opt.winId;
    }
    var winFId = winId + "_frame";
    if (document.getElementById(winId)) {
        return;
    }
    var s = '<div id="' + winId + '" class="easyui-window" title="Modal Window" data-options="resizable:true,collapsible:false,maximizable:true,minimizable:false,closable:true,modal:true,closed:true,iconCls:\'icon-save\'"';
    s += ' style="width: 430px; height: 170px; padding: 10px;">';
    s += '<iframe scrolling="auto" id="' + winFId + '" frameborder="0" src="" style="width: 100%; height: 100%;"></iframe>';
    s += "</div>";
    $("body").append(s);
};
function setCenter(width, height, opt) {
    var bIsTop = self == top;
    if (!bIsTop) {
        if (typeof (opt) == "object" && typeof (opt.isInTop) == "boolean" && opt.isInTop) {
            top.currentWin = window;
            top.setCenter(width, height, opt);
            return;
        }
    }
    var winId = "globall_pop_win_9981";
    if (typeof (opt) == "object" && typeof (opt.winId) == "string") {
        winId = opt.winId;
    }

    var winFId = winId + "_frame";
    var oSize = getPageSize();
    var winWidth = oSize.WindowWidth;
    var winHeight = oSize.WindowHeight;
    var iLeft = parseInt((winWidth - width) / 2, 10);
    var iTop = parseInt((winHeight - height) / 2, 10);
    if (iLeft <= 0) { iLeft = 0; }
    if (iTop <= 0) { iTop = 0; }
    $('#' + winId).window(
    {
        left: iLeft,
        top: iTop
    }
    );
};

function openWin(url, width, height, title, opt) {
    openAddWin(url, width, height, title, opt);
};

function openAddWinInTop(url, width, height, title) {
    var opt = { isInTop: true };
    openAddWin(url, width, height, title, opt);
};

function openAddWin(url, width, height, title, opt) {
    if (typeof (url) != "string") {
        alert("url不正确！");
        return
    }

    var bIsTop = self == top;
    if (!bIsTop) {
        if (typeof (opt) == "object" && typeof (opt.isInTop) == "boolean" && opt.isInTop) {
            top.currentWin = window;
            top.openAddWin(url, width, height, title, opt);
            return;
        }
    }
    var winId = "globall_pop_win_9981";
    if (typeof (opt) == "object" && typeof (opt.winId) == "string") {
        winId = opt.winId;
    }

    var winFId = winId + "_frame";
    createWin(opt);
    var iWidth = 400;
    var iHeight = 300;

    if (typeof (width) == "number") {
        iWidth = parseInt(width, 10);
    }
    if (typeof (height) == "number") {
        iHeight = parseInt(height, 10);
    }

    setCenter(width, height, opt);
    $('#' + winFId)[0].src = url;
    $('#' + winId).window({
        title: title,
        width: iWidth,
        height: iHeight
    }
    );
    $('#' + winId).window('open');
};

function openEditTabWin(url, width, height, title, sGrid, keyField, editTabId) {
    var myGrid99765 = null;
    eval("myGrid99765 = " + sGrid + ";");

    var objGrid = myGrid99765;

    var count = objGrid.GetCheckedRowCount();
    if (count == 0) {
        $.messager.alert('提示', '<br>请选择要修改的记录！<br>', 'info');
        return;
    }

    if (count != 1) {
        $.messager.alert('提示', '<br>一次只能修改一条记录！<br>', 'info');
        return;
    }
    var ids = objGrid.GetCheckedRows("chkValue");
    if (url.indexOf("?") == -1) {
        url += "?" + keyField + "=" + ids;
    }
    else {
        url += "&" + keyField + "=" + ids;
    }
    openTab(editTabId + ids, title, url)
};

function openEditWinInTop(url, width, height, title, sGrid, keyField, isInTab, editTabId) {
    var opt = { isInTop: true };
    openEditWin(url, width, height, title, sGrid, keyField, isInTab, editTabId, opt);
};

function openEditWin(url, width, height, title, sGrid, keyField, isInTab, editTabId, opt) {
    if (typeof (url) != "string") {
        alert("url不正确！");
        return
    }
    if (typeof (isInTab) == "boolean") {
        if (isInTab) {
            openEditTabWin(url, width, height, title, sGrid, keyField, editTabId);
            return;
        }
    }
    var myGrid99765 = null;
    eval("myGrid99765 = " + sGrid + ";");
    var objGrid = myGrid99765;
    var count = objGrid.GetCheckedRowCount();
    if (count == 0) {
        $.messager.alert('提示', '<br>请选择要修改的记录！<br>', 'info');
        return;
    }
    if (count != 1) {
        $.messager.alert('提示', '<br>一次只能修改一条记录！<br>', 'info');
        return;
    }
    var ids = objGrid.GetCheckedRows("chkValue");

    if (url.indexOf("?") == -1) {
        url += "?" + keyField + "=" + ids;
    }
    else {
        url += "&" + keyField + "=" + ids;
    }
    openAddWin(url, width, height, title, opt)
};

function closeWin(wid) {
    try {
        var winId = "globall_pop_win_9981";
        if (typeof (wid) == "string") {
            winId = wid;
        }
        var winFId = winId + "_frame";
        $('#' + winId).window('close');
        $('#' + winFId)[0].src = "";
    }
    catch (e) {
        var winId = "globall_pop_win_9981";

        var winFId = winId + "_frame";
        $('#' + winId).window('close');
        $('#' + winFId)[0].src = "";
    }
};

function refreshGridData(sGrid) {
    var objGrid = null;
    if (typeof (sGrid) == "string") {
        var myGrid99765 = null;
        eval("myGrid99765 = " + sGrid + ";");
        objGrid = myGrid99765;
    }
    else {
        var myGrid9976522 = null;
        eval("myGrid9976522 = " + "grid" + ";");
        objGrid = myGrid9976522;
    }
    if (objGrid != null) {
        if ($("#GridSearchForm").size() > 0) {
            gridSearch(sGrid, true);
        }
        else {
            var qC = {};
            if (typeof (querys) == "object") {
                for (var attName in querys) {
                    qC[attName] = querys[attName];
                }
            }
            objGrid.ReLoad(qC);
            objGrid.SellAll(false);
        }
        //alert("1");
        $("input[type=checkbox]").attr("checked", false);
        //alert("2");
    }
};


function refreshGridData2(sGrid) {
    var objGrid = null;
    if (typeof (sGrid) == "string") {
        var myGrid99765 = null;
        eval("myGrid99765 = " + sGrid + ";");
        objGrid = myGrid99765;
    }
    else {
        var myGrid9976522 = null;
        eval("myGrid9976522 = " + "grid" + ";");
        objGrid = myGrid9976522;
    }
    if (objGrid != null) {
        var qC = {};
        var queryParams = new Object();
        if (document.getElementById("GridSearchForm")) {
            var fields = $("#GridSearchForm").serialize(); // Array
            var arr1 = fields.split("&");
            for (var arrIndex1 = 0; arrIndex1 < arr1.length; arrIndex1++) {
                var arr2 = arr1[arrIndex1].split("=");
                if (typeof (queryParams[arr2[0]]) == "string") {
                    queryParams[arr2[0]] = queryParams[arr2[0]] + "," + arr2[1];
                }
                else {
                    queryParams[arr2[0]] = arr2[1];
                }
            }
        }
        objGrid.ReLoad(queryParams);
        objGrid.SellAll(false);
        //alert("1");
        $("input[type=checkbox]").attr("checked", false);
        //alert("2");
    }
};

function CustomerLockData(sGrid, delUrl, keyField) {
    var objGrid = null;
    if (typeof (sGrid) == "string") {
        var myGrid99765 = null;
        eval("myGrid99765 = " + sGrid + ";");
        objGrid = myGrid99765;
    }
    else {
        var myGrid9976522 = null;
        eval("myGrid9976522 = " + "grid" + ";");
        objGrid = myGrid9976522;
    }
    if (objGrid == null) {
        alert("Grid对象为空！");
        return;
    }
    var count = objGrid.GetCheckedRowCount();
    if (count == 0) {
        $.messager.alert('提示', '<br>请选择要禁用的记录！<br>', 'info');
        return;
    }
    $.messager.confirm('确认提示', '<br>您确认要禁用所选记录吗？', function () {
        var b = arguments[0];
        if (b) {
            doLockChkData();
        }
    });

    function doLockChkData() {
        var ids = objGrid.GetCheckedRows("chkValue");
        var data = new Object();
        data[keyField] = ids;
        $.post(delUrl, data, function (resp) { LockCallback(resp); });
        function LockCallback(resp) {
            if (resp.RetCode == 1) {
                $.messager.alert('提示', '<br>' + resp.RetMsg == undefined ? '禁用成功！' : resp.RetMsg + '<br>', 'info');
                refreshGridData(sGrid);
            }
            else {
                $.messager.alert('提示', '<br>' + resp.RetMsg == undefined ? '禁用失败！' : resp.RetMsg + '<br>', 'info');
            }
        };
    };
};


function deleteData(sGrid, delUrl, keyField) {
    var objGrid = null;
    if (typeof (sGrid) == "string") {
        var myGrid99765 = null;
        eval("myGrid99765 = " + sGrid + ";");
        objGrid = myGrid99765;
    }
    else {
        var myGrid9976522 = null;
        eval("myGrid9976522 = " + "grid" + ";");
        objGrid = myGrid9976522;
    }
    if (objGrid == null) {
        alert("Grid对象为空！");
        return;
    }
    var count = objGrid.GetCheckedRowCount();
    if (count == 0) {
        $.messager.alert('提示', '<br>请选择要删除的记录！<br>', 'info');
        return;
    }
    $.messager.confirm('确认提示', '<br>您确认要删除所选记录吗？', function () {
        var b = arguments[0];
        if (b) {
            doDelChkData();
        }
    });



    function doDelChkData() {
        var ids = objGrid.GetCheckedRows("chkValue");
        var data = new Object();
        data[keyField] = ids;
        $.post(delUrl, data, function (resp) { delCallback(resp); });
        function delCallback(resp) {
            if (resp.RetCode == 1) {
                $.messager.alert('提示', '<br>' + resp.RetMsg == undefined ? '删除成功！' : resp.RetMsg + '<br>', 'info');
                refreshGridData(sGrid);
            }
            else {
                $.messager.alert('提示', '<br>' + resp.RetMsg == undefined ? '删除失败！' : resp.RetMsg + '<br>', 'info');
            }
        };
    };
};


function deleteDataForACS(sGrid, delUrl, keyField) {
    var objGrid = null;
    if (typeof (sGrid) == "string") {
        var myGrid99765 = null;
        eval("myGrid99765 = " + sGrid + ";");
        objGrid = myGrid99765;
    }
    else {
        var myGrid9976522 = null;
        eval("myGrid9976522 = " + "grid" + ";");
        objGrid = myGrid9976522;
    }
    if (objGrid == null) {
        alert("Grid对象为空！");
        return;
    }
    var count = objGrid.GetCheckedRowCount();
    if (count == 0) {
        $.messager.alert('提示', '<br>请选择要删除的记录！<br>', 'info');
        return;
    }
    $.messager.confirm('确认提示', '<br>确定删除当前整个群组以及包含的所有站点？', function () {
        var b = arguments[0];
        if (b) {
            doDelChkDataForACS();
        }
    });

    function doDelChkDataForACS() {
        var ids = objGrid.GetCheckedRows("chkValue");
        var data = new Object();
        data[keyField] = ids;
        $.post(delUrl, data, function (resp) { delCallbackForACS(resp); });
        function delCallbackForACS(resp) {
            if (resp.RetCode == 1) {
                $.messager.alert('提示', '<br>' + resp.RetMsg == undefined ? '删除成功！' : resp.RetMsg + '<br>', 'info');
                refreshGridData(sGrid);
            }
            else {
                $.messager.alert('提示', '<br>' + resp.RetMsg == undefined ? '删除失败！' : resp.RetMsg + '<br>', 'info');
            }
        };
    };
};

function getSearchCondition(sGrid) {
    var objGrid = null;
    if (typeof (sGrid) == "string") {
        var myGrid99765 = null;
        eval("myGrid99765 = " + sGrid + ";");
        objGrid = myGrid99765;
    }
    else {
        var myGrid9976522 = null;
        eval("myGrid9976522 = " + "grid" + ";");
        objGrid = myGrid9976522;
    }
    if (objGrid == null) {
        alert("Grid对象为空！");
        return;
    }
    var queryParams = new Object();
    if (document.getElementById("GridSearchForm")) {
        var fields = decodeURIComponent($("#GridSearchForm").serialize()); // Array
        var arr1 = fields.split("&");
        for (var arrIndex1 = 0; arrIndex1 < arr1.length; arrIndex1++) {
            var arr2 = arr1[arrIndex1].split("=");
            if (typeof (queryParams[arr2[0]]) == "string") {
                queryParams[arr2[0]] = queryParams[arr2[0]] + "," + arr2[1];
            }
            else {
                queryParams[arr2[0]] = arr2[1];
            }
        }
    }
    if (typeof (querys) == "object") {
        for (var attName in querys) {
            var pageName = getPageName();
            if (pageName == "stationlist") {
                if (attName != "ProjectId") {
                    queryParams[attName] = querys[attName];
                }
            }
            else {
                queryParams[attName] = querys[attName];
            }
        }
    }
    return queryParams;
};
function gridSearch(sGrid, isRefresh) {

    if (typeof (searchDataCheck) == "function") {
        if (!searchDataCheck()) {
            return;
        }
    }
    var objGrid = null;
    if (typeof (sGrid) == "string") {
        var myGrid99765 = null;
        eval("myGrid99765 = " + sGrid + ";");
        objGrid = myGrid99765;
    }
    else {
        var myGrid9976522 = null;
        eval("myGrid9976522 = " + "grid" + ";");
        objGrid = myGrid9976522;
    }
    if (objGrid == null) {
        alert("Grid对象为空！");
        return;
    }

    var queryParams = new Object();
    if (document.getElementById("GridSearchForm")) {
        var fields = decodeURIComponent($("#GridSearchForm").serialize(), true); // Array

        var arr1 = fields.split("&");
        for (var arrIndex1 = 0; arrIndex1 < arr1.length; arrIndex1++) {
            var arr2 = arr1[arrIndex1].split("=");
            if (typeof (queryParams[arr2[0]]) == "string") {

                queryParams[arr2[0]] = queryParams[arr2[0]] + "," + arr2[1];
            }
            else {
                queryParams[arr2[0]] = arr2[1];
            }
        }
    }
    if (typeof (querys) == "object") {
        for (var attName in querys) {
            var pageName = getPageName();
            if (pageName == "stationlist") {
                if (attName != "ProjectId") {
                    queryParams[attName] = querys[attName];
                }
            }
            else {
                queryParams[attName] = querys[attName];
            }
        }
    }
    objGrid.Load(queryParams);
};

function createBtn(opt) {
    if (typeof (opt) != "object") {
        return;
    }
    if ((!opt.ShowAddBtn) && (!opt.ShowEditBtn) && (!opt.ShowDelBtn) && (!opt.ShowRefreshBtn) && (!opt.ShowCustomBtn) && (!opt.ShowCustomCombobox)) {
        if (typeof (opt.Html) != "string" || $.trim(opt.Html) == "") {
            if (typeof (opt.SearchHtml) != "string" || $.trim(opt.SearchHtml) == "") {
                if (typeof (opt.Search) != "object" || opt.Search.length == 0) {
                    return;
                }
            }
        }
    }
    var s = '<div id="tb" style="padding: 5px; height: auto">'
    if (typeof (opt.ToolbarId) == "string") {
        s = '<div id="' + opt.ToolbarId + '" style="padding: 5px; height: auto">'
    }
    var openWinInTop = false;
    if (typeof (opt.OpenWinInTop) == "boolean") {
        openWinInTop = opt.OpenWinInTop;
    }

    s += '<div style="margin-bottom: 5px">';

    if (opt.ShowAddBtn) {
        if (opt.EditInTab) {
            s += '<a href="javascript:;" onclick="openTab(\'' + opt.EditTabId + '0\',\'' + opt.AddTitle + '\', \'' + opt.AddUrl + '\');" class="easyui-linkbutton" iconcls="icon-add" plain="true">添加</a> ';
        }
        else {
            if (!openWinInTop) {
                s += '<a href="javascript:;" onclick="openAddWin(\'' + opt.AddUrl + '\',' + opt.AddWinWidth + ',' + opt.AddWinHeight + ',\'' + opt.AddTitle + '\');" class="easyui-linkbutton" iconcls="icon-add" plain="true">添加</a> ';
            }
            else {
                s += '<a href="javascript:;" onclick="openAddWinInTop(\'' + opt.AddUrl + '\',' + opt.AddWinWidth + ',' + opt.AddWinHeight + ',\'' + opt.AddTitle + '\');" class="easyui-linkbutton" iconcls="icon-add" plain="true">添加</a> ';
            }
        }
    }
    if (opt.ShowEditBtn) {
        if (opt.EditInTab) {
            s += '<a href="javascript:;" onclick="openEditWin(\'' + opt.EditUrl + '\',' + opt.EditWinWidth + ',' + opt.EditWinHeight + ',\'' + opt.EditTitle + '\',\'' + opt.GridId + '\',\'' + opt.EditKeyField + '\',true,\'' + opt.EditTabId + '\');" class="easyui-linkbutton" iconcls="icon-edit" plain="true">修改</a> ';
        }
        else {
            if (!openWinInTop) {
                s += '<a href="javascript:;" onclick="openEditWin(\'' + opt.EditUrl + '\',' + opt.EditWinWidth + ',' + opt.EditWinHeight + ',\'' + opt.EditTitle + '\',\'' + opt.GridId + '\',\'' + opt.EditKeyField + '\');" class="easyui-linkbutton" iconcls="icon-edit" plain="true">修改</a> ';
            }
            else {
                s += '<a href="javascript:;" onclick="openEditWinInTop(\'' + opt.EditUrl + '\',' + opt.EditWinWidth + ',' + opt.EditWinHeight + ',\'' + opt.EditTitle + '\',\'' + opt.GridId + '\',\'' + opt.EditKeyField + '\');" class="easyui-linkbutton" iconcls="icon-edit" plain="true">修改</a> ';
            }
        }
    }
    if (opt.ShowDelBtn) {
        s += '<a href="javascript:;" onclick="deleteData(\'' + opt.GridId + '\',\'' + opt.DelUrl + '\',\'' + opt.DelKeyField + '\');" class="easyui-linkbutton" iconcls="icon-remove" plain="true">删除</a>';
    }
    if (opt.ShowCustomDelBtn) {
        s += '<a href="javascript:;" onclick="deleteDataForACS(\'' + opt.GridId + '\',\'' + opt.DelUrl + '\',\'' + opt.DelKeyField + '\');" class="easyui-linkbutton" iconcls="icon-remove" plain="true">删除群组</a>';
    }
    //增加自定义下拉框
    if (opt.ShowCustomCombobox) {
        s += '<input id="' + opt.CustomComboboxID + '" class="easyui-combobox" />';
    }
    //增加自定义按钮
    if (opt.ShowCustomBtn) {
        s += '<a href="javascript:;" onclick="customAction(\'' + opt.GridId + '\',\'' + opt.CustomUrl + '\',\'' + opt.CustomKeyField + '\');" class="easyui-linkbutton" iconcls="icon-edit" plain="true">' + opt.CustomTitle + '</a>';
    }
    if (opt.ShowLockBtn) {
        s += '<a href="javascript:;" onclick="CustomerLockData(\'' + opt.GridId + '\',\'' + opt.LockUrl + '\',\'' + opt.LockKeyField + '\');" class="easyui-linkbutton" iconcls="icon-lock" plain="true">' + opt.LockTitle + '</a>';
    }

    if (opt.ShowRefreshBtn) {
        s += '<a href="javascript:;" onclick="refreshGridData(\'' + opt.GridId + '\');" class="easyui-linkbutton" iconcls="icon-reload" plain="true">刷新</a>';
    }
    if (typeof (opt.Html) == "string" && $.trim(opt.Html) != "") {
        s += $.trim(opt.Html);
    }
    s += '</div>';

    //alert(s);
    if (typeof (opt.SearchHtml) == "string" && $.trim(opt.SearchHtml) != "") {
        s += '<div class="searchClass"><form id="GridSearchForm" name="GridSearchForm">' + $.trim(opt.SearchHtml) + '</form></div>';
    }
    else {
        if (typeof (opt.Search) == "object") {
            var sHtml = "";
            for (var sIndex = 0; sIndex < opt.Search.length; sIndex++) {
                var curOpt = opt.Search[sIndex];
                sHtml += "  &nbsp;&nbsp;" + curOpt.Text + "：" + curOpt.InputBox;
            }
            if (sHtml != "") {
                sHtml += " <a href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-search\" onclick=\"gridSearch('grid');\">查询</a>";
                sHtml = "<div><form id=\"GridSearchForm\" name=\"GridSearchForm\">" + sHtml + "</form</div>";
                s += sHtml;
            }
        }
    }
    s += '</div>';
    document.write(s);
};


function trigerListWinResize() {
    $(window).trigger("resize");
};
$(function () {
    (function () {
        if (document.getElementById("listDataEx1")) {
            trigerListWinResize();
            var oldWind = window.onload;
            window.onload = function () {
                if (oldWind) {
                    oldWind();
                }
                $(window).trigger("resize");
            };
        }
    })();
});

/* 导出Excel公共方法 */
function pubExportToExcel(url) {
    // debugger;
    if (!document.getElementById("listDataEx1")) {
        $.messager.alert('提示', '<br>没有可导出的数据！<br>', 'info', function () { });
        return;
    }
    if (!($("#listDataEx1").datagrid)) {
        $.messager.alert('提示', '<br>没有可导出的数据！<br>', 'info', function () { });
        return;
    }
    try {
        var rows = $("#listDataEx1").datagrid("getData");
    } catch (e) {
        $.messager.alert('提示', '<br>没有可导出的数据！<br>', 'info', function () { });
        return;
    }
    var rows = $("#listDataEx1").datagrid("getData");
    if (!rows || !rows.total) {
        $.messager.alert('提示', '<br>没有可导出的数据！<br>', 'info', function () { });
        return;
    }
    if (rows.total <= 0) {
        $.messager.alert('提示', '<br>没有可导出的数据！<br>', 'info', function () { });
        return;
    }
    //  debugger;
    var qs = getSearchCondition();
    top.exportToExcel(url, qs);
};


